

package Control;

import Drawing.TankImage;
import Game.Match;
import Game.Shot;
import Game.Tank;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;

/**
 * This class is the class that the user interacts with.
 * This is te top level class, it will not have any listeners attached to it. Instead
 * it will control all the player movements
 * Compiler: jdk1.6.0_14 <br />
 * IDE: NetBeans 6.7.1 <br />
 * @author Andrew Smith <br />
 */
public class Controller extends JPanel
{
    /** the width of the panel */
    private static final int width = Match.BOUNDS_WIDTH;
    /** the height of the panel */
    private static final int height = Match.BOUNDS_HEIGHT;

    /** the match that the player is in */
    private Match match;
    /** the players tank */
    private Tank tank;
    /** the tank image */
    private TankImage img;
    /** listener for updates to the match */
    private Listener listener;


    /**
     * Makes a new controller
     * @param name the name of the player
     * @param match the match that this player will enter
     */
    public Controller(String name, Match match)
    {
        setup();
        tank = new Tank(name, 50, 50);
        tank.addActionListener(listener);
        img = new TankImage(tank);
        this.match = match;
        match.addTank(tank);
        match.addActionListener(listener);
    }

    /**
     * Sets up the controller
     */
    private void setup()
    {
        listener = new Listener();
        setPreferredSize(new Dimension(width, height));
        this.setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(new KeyboardListener());
    }





    @Override
    public void paint(Graphics g)
    {
        super.paintComponent(g);

        match.paint(g);
        g.setColor(Color.WHITE);
        g.drawString(Match.VERSION_NO, 10, height-10);
    }


    /**
     * Inner class to listen for all updates
     */
    private class Listener implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {
            repaint();
        }

    }



    private class KeyboardListener implements KeyListener
    {

        public void keyTyped(KeyEvent e)
        {
            //does nothing
        }

        public void keyPressed(KeyEvent e)
        {
            int key = e.getKeyCode();



            boolean canMove = true;

            if(canMove)
            {
                if(key == KeyEvent.VK_UP || key == KeyEvent.VK_W) //this is move fowards
                {
                    tank.moveForwards();
                    if(!match.safeMove(img.getPointsX(), img.getPointsY()))
                    {   tank.moveBackwards();
                    }
                }
                if(key == KeyEvent.VK_A || key == KeyEvent.VK_LEFT) //this is move left
                {
                    tank.rotateLeft();
                    if(!match.safeMove(img.getPointsX(), img.getPointsY()))
                    {   tank.rotateRight();
                    }
                }
                if(key == KeyEvent.VK_D || key == KeyEvent.VK_RIGHT)
                {
                    tank.rotateRight();
                    if(!match.safeMove(img.getPointsX(), img.getPointsY()))
                    {   tank.rotateLeft();
                    }
                }
                if(key == KeyEvent.VK_S || key == KeyEvent.VK_DOWN)
                {
                    tank.moveBackwards();
                    if(!match.safeMove(img.getPointsX(), img.getPointsY()))
                    {   tank.moveForwards();
                    }
                }
                if(key == KeyEvent.VK_SPACE)
                {
                    tank.shoot();
                    tank.getShot().addActionListener(new ShotListener());
                }
                repaint();
            }
        }

        public void keyReleased(KeyEvent e)
        {
            //does nothing
        }

    }


    private class ShotListener implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() instanceof Shot)
            {
                if(!e.getActionCommand().equalsIgnoreCase(Shot.HIT_OBSTACLE_MESSAGE))
                {
                    Shot shot = (Shot) e.getSource();
                    int[] xs = {shot.getX() };
                    int[] ys = {shot.getY() };
                    if(!match.safeMove(xs, ys)) //if it hits a rock
                    {
                        shot.hitObstacle();
                    }
                }


            }
            repaint();
        }

    }



}
